package com.song.secure;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.stereotype.Repository;

import com.song.secure.service.RoleService;
public class CustomFilterInvocationSecurityMetadataSourceImpl implements
		CustomFilterInvocationSecurityMetadataSource {
	
	@Resource
	private RoleService roleService;
	
	private Map<String, Collection<ConfigAttribute>> resourceMap = null;
	
	@PostConstruct
	public void init()
	{
		loadResource();
	}
	
	public void  loadResource()
	{
		resourceMap = this.roleService.getAllConfigAttribute();
	}
	
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}

	public Collection<ConfigAttribute> getAttributes(Object obj)
			throws IllegalArgumentException {
		String url = ((FilterInvocation) obj).getRequestUrl().trim();
		if ("/".equals(url)) {
			return null;
		}
		int firstQuestionMarkIndex = url.indexOf(".");
		if (firstQuestionMarkIndex != -1) {
			url = url.substring(0, firstQuestionMarkIndex);
		}
		//�����γɵ�uri ��ʽ:/index

		Iterator<String> ite = resourceMap.keySet().iterator();
		// ȡ�������URL��������ȡ��������Դ���Ƚ�
		while (ite.hasNext()) {
			String resURL = ite.next().trim();
			if (url.equals(resURL)) {
				//��ø�uri����Ҫ�Ľ�ɫ�б�
				return resourceMap.get(resURL);
			}
		}
		//�����ݿ�����û�и�uri����Ϣ����ʾ��uri����Ҫ����Ȩ����֤��
		return null;
	}

	public boolean supports(Class<?> arg0) {
		return true;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	
}
